
package session.stateless.MRP.ProductionPlan;


import entity.MRP.ProductionPlan.PlannedDailyDemand;
import entity.MRP.ProductionPlan.PlannedMonthlyDemand;
import entity.MRP.ProductionPlan.PlannedWeeklyDemand;
import entity.MRP.SOProcess.BackOrder;
import entity.MRP.SOProcess.WeeklyDemandAmendment;
import entity.Product;
import java.util.ArrayList;
import java.util.List;
import javaclass.ProductType;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import session.stateless.HolidayBeanLocal;


@Stateless
public class DailyProductionPlanBean implements DailyProductionPlanBeanLocal {

    @PersistenceContext 
    private EntityManager em;
    @EJB
    private WeeklyProductionPlanBeanLocal weeklyProductionPlanBeanLocal;
    @EJB
    private HolidayBeanLocal holidayBeanLocal;
    
    public List<PlannedDailyDemand> createPlannedDailyDemands(String week)
    {
        List<PlannedDailyDemand> dayList = new ArrayList<PlannedDailyDemand>();
        int startDay = Integer.parseInt(week.substring(0, week.indexOf("/")));
        String temp = week.substring(week.indexOf("/") + 1);
        int endDay = Integer.parseInt(temp.substring(temp.indexOf("-")+1, temp.indexOf("/")));
        int month = Integer.parseInt(temp.substring(temp.indexOf("/") + 1));

        //sett WeeklyDemand
        Query q = em.createQuery("SELECT p FROM PlannedWeeklyDemand p WHERE p.weekPeriod=:week");
        q.setParameter("week", week);
        PlannedWeeklyDemand plannedWeeklyDemand = (PlannedWeeklyDemand) q.getSingleResult();
        PlannedMonthlyDemand plannedMonthlyDemand = plannedWeeklyDemand.getPlannedMonthlyDemand();
        
        Long monthlyDemandA = new Long(0);
        Long monthlyDemandB = new Long(0);
        Long monthlyDemandC = new Long(0);
        Long monthlyDemandD = new Long(0);
        
        
        if (plannedMonthlyDemand.getProductionPlanA() != null)
        {
            monthlyDemandA = plannedMonthlyDemand.getProductionPlanA();
        }
        if (plannedMonthlyDemand.getProductionPlanB() != null)
        {
            monthlyDemandB = plannedMonthlyDemand.getProductionPlanB();
        }
        if (plannedMonthlyDemand.getProductionPlanC() != null)
        {
            monthlyDemandC = plannedMonthlyDemand.getProductionPlanC();
        }
        if (plannedMonthlyDemand.getProductionPlanD() != null)
        {
            monthlyDemandD = plannedMonthlyDemand.getProductionPlanD();
        }
        
        Long workingDays = plannedMonthlyDemand.getNumberOfWorkingDays();
        String dayName;
        String yearName;
        for (int i = startDay; i <= endDay; ++i)
        {
            dayName = Integer.toString(i) + "/" + Integer.toString(month);
            yearName = plannedMonthlyDemand.getMonthName().substring(plannedMonthlyDemand.getMonthName().indexOf("/") + 1);
            

            if(!holidayBeanLocal.isHoliday(dayName, yearName))
            {
                PlannedDailyDemand p = new PlannedDailyDemand();
                p.setPlannedWeeklyDemand(plannedWeeklyDemand);
                p.setDayName(dayName);

                p.setDailyDemandA(Math.round((double) monthlyDemandA / (double) workingDays));
                p.setDailyDemandB(Math.round((double) monthlyDemandB / (double) workingDays));
                p.setDailyDemandC(Math.round((double) monthlyDemandC / (double) workingDays));
                p.setDailyDemandD(Math.round((double) monthlyDemandD / (double) workingDays));

                em.persist(p);

                em.flush();

                dayList.add(p);
            }
        }        
        
        return dayList;
    }
    
    public void updatePlannedDailyDemands(Long id)
    {
        PlannedDailyDemand p = em.find(PlannedDailyDemand.class, id);
        String date = p.getDayName();
        int day = Integer.parseInt(date.substring(0, date.indexOf("/")));
        String temp = date.substring(date.indexOf("/") + 1);
        int month = Integer.parseInt(temp.substring(temp.indexOf("/") + 1));

        PlannedWeeklyDemand plannedWeeklyDemand = p.getPlannedWeeklyDemand();
        PlannedMonthlyDemand plannedMonthlyDemand = plannedWeeklyDemand.getPlannedMonthlyDemand();
        
        Long monthlyDemandA = plannedMonthlyDemand.getProductionPlanA();
        Long monthlyDemandB = plannedMonthlyDemand.getProductionPlanB();
        Long monthlyDemandC = plannedMonthlyDemand.getProductionPlanC();
        Long monthlyDemandD = plannedMonthlyDemand.getProductionPlanD();
        
        Long workingDays = plannedMonthlyDemand.getNumberOfWorkingDays();

            
        p.setDailyDemandA(Math.round((double) monthlyDemandA / (double) workingDays));
        p.setDailyDemandB(Math.round((double) monthlyDemandB / (double) workingDays));
        p.setDailyDemandC(Math.round((double) monthlyDemandC / (double) workingDays));
        p.setDailyDemandD(Math.round((double) monthlyDemandD / (double) workingDays));

        em.flush();
    }
    
    public List<PlannedDailyDemand> getDailyDemands(String week)
    {
        Query q = em.createQuery("SELECT p FROM PlannedWeeklyDemand p WHERE p.weekPeriod =:week");
        q.setParameter("week", week);
        PlannedWeeklyDemand p = (PlannedWeeklyDemand) q.getSingleResult();
        List<PlannedDailyDemand> list = p.getPlannedDailyDemand();
        return list;
    }
    
    @Override
    public void updatePlannedDailyDemandsWithQuantity(Long id, List<Product> products, List<Long> quantities, int numOfDays, Long boId){
        PlannedDailyDemand p = em.find(PlannedDailyDemand.class, id);
        
        Long demandA = (long)0;
        Long demandB = (long)0;
        Long demandC = (long)0;
        Long demandD = (long)0;
        
        for(int i = 0; i <= products.size() - 1; i++){
            if (products.get(i).getProductType() == ProductType.NUTS){
                demandA = quantities.get(i);
            }
            if (products.get(i).getProductType() == ProductType.FRUITS){
                demandB = quantities.get(i);
            }
            if (products.get(i).getProductType() == ProductType.CHOCOLATE_NUTS){
                demandC = quantities.get(i);
            }
            if (products.get(i).getProductType() == ProductType.REDCURRANT_FRUITS){
                demandD = quantities.get(i);
            }
        }
        
        Long amtA = Math.round((double) demandA / (double) numOfDays);
        Long amtB = Math.round((double) demandB / (double) numOfDays);
        Long amtC = Math.round((double) demandC / (double) numOfDays);
        Long amtD = Math.round((double) demandD / (double) numOfDays);
        
        p.setDailyDemandA(p.getDailyDemandA() + amtA);
        p.setDailyDemandB(p.getDailyDemandB() + amtB);
        p.setDailyDemandC(p.getDailyDemandC() + amtC);
        p.setDailyDemandD(p.getDailyDemandD() + amtD);

        PlannedWeeklyDemand w = p.getPlannedWeeklyDemand();
        w.setWeeklyDemandA(w.getWeeklyDemandA() + amtA);
        w.setWeeklyDemandA(w.getWeeklyDemandB() + amtB);
        w.setWeeklyDemandA(w.getWeeklyDemandC() + amtC);
        w.setWeeklyDemandA(w.getWeeklyDemandD() + amtD);
        
        if (w.getWeeklyDemandAmendment() == null){
            WeeklyDemandAmendment wDA = new WeeklyDemandAmendment();
            BackOrder bo = em.find(BackOrder.class, boId);
            wDA.setBackOrder(bo);
            wDA.setPlannedWeeklyDemand(w);
            
            List<ProductType> pType = new ArrayList<ProductType>();
            pType.add(ProductType.NUTS);
            pType.add(ProductType.FRUITS);
            pType.add(ProductType.CHOCOLATE_NUTS);
            pType.add(ProductType.REDCURRANT_FRUITS);
            
            wDA.setProductTypes(pType);
            
            List<Long> qty = new ArrayList<Long>();
            qty.add(amtA);
            qty.add(amtB);
            qty.add(amtC);
            qty.add(amtD);
            
            wDA.setQuantities(quantities);
            em.persist(wDA);
        }
        else{
            WeeklyDemandAmendment wDA = w.getWeeklyDemandAmendment();
            BackOrder bo = em.find(BackOrder.class, boId);
            wDA.setBackOrder(bo);
            wDA.setPlannedWeeklyDemand(w);
            
            List<ProductType> pType = new ArrayList<ProductType>();
            pType.add(ProductType.NUTS);
            pType.add(ProductType.FRUITS);
            pType.add(ProductType.CHOCOLATE_NUTS);
            pType.add(ProductType.REDCURRANT_FRUITS);
            
            wDA.setProductTypes(pType);
            
            List<Long> qty = new ArrayList<Long>();
            qty.add(wDA.getQuantities().get(0) + amtA);
            qty.add(wDA.getQuantities().get(1) + amtB);
            qty.add(wDA.getQuantities().get(2) + amtC);
            qty.add(wDA.getQuantities().get(3) + amtD);
            
            wDA.setQuantities(quantities);
            em.flush();
        }
        
        PlannedMonthlyDemand m = w.getPlannedMonthlyDemand();
        m.setProductionPlanA(m.getProductionPlanA() + Math.round((double) demandA / (double) numOfDays));
        m.setProductionPlanB(m.getProductionPlanB() + Math.round((double) demandB / (double) numOfDays));
        m.setProductionPlanC(m.getProductionPlanC() + Math.round((double) demandC / (double) numOfDays));
        m.setProductionPlanD(m.getProductionPlanD() + Math.round((double) demandD / (double) numOfDays));
        m.setProductionPlan(m.getProductionPlanA() + m.getProductionPlanB() + m.getProductionPlanC() + m.getProductionPlanD());
        
        em.flush();
    }
}
